package com.andy.beef.app

import com.alibaba.android.arouter.launcher.ARouter
import com.andy.beef.R
import com.andy.beef.app.net.AppHeadInterceptor
import com.andy.module.loadsir.EmptyCallback
import com.andy.module.loadsir.ErrorCallback
import com.andy.module.loadsir.LoadingCallback
import com.andy.module.network.RetrofitClient
import com.blankj.utilcode.util.Utils
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.tencent.mmkv.MMKV
import me.hgj.jetpackmvvm.base.BaseApp

/**
 * @creator:LuoFan
 * @date:2022/5/25
 * @description:
 */
class App : BaseApp() {

    companion object {
        lateinit var instance: App
    }

    override fun onCreate() {
        super.onCreate()
        MMKV.initialize(this)
        instance = this
        Utils.init(this)
        initRefresh()
        ARouter.init(this)
        initLoadSir()
        RetrofitClient.headerInterceptor = AppHeadInterceptor()
    }

    private fun initLoadSir() {
        LoadSir.beginBuilder()
            .addCallback(ErrorCallback()) //添加各种状态页
            .addCallback(LoadingCallback())
            .addCallback(EmptyCallback())
            .setDefaultCallback(LoadingCallback::class.java) //设置默认状态页
            .commit()
    }

    private fun initRefresh() {
        SmartRefreshLayout.setDefaultRefreshInitializer { context, layout ->
            layout.setEnableLoadMoreWhenContentNotFull(true)
        }
        SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
            //全局设置主题颜色
            layout.setPrimaryColorsId(R.color.transparent, R.color.refresh_text_color)
            ClassicsHeader(context).setProgressResource(R.mipmap.refresh_loading)
        }

        SmartRefreshLayout.setDefaultRefreshFooterCreator { context, layout ->
            ClassicsFooter(context).setProgressResource(R.mipmap.refresh_loading)
        }
    }
}